﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using Service;

namespace ECommerce.Administration.Controls
{
	public partial class EditProductReview : AdminBaseUsercontrol
	{
		private IProductReviewService _ProductReviewService;
		private IProductService _ProductService;

		private void BindProducts()
		{
			if (SiteContext.Current.WorkingLanguage != null)
			{
				if (_ProductReviewService == null)
				{
					_ProductReviewService = new Service.ProductReviewService();
				}

				var productReviews = _ProductReviewService.GetAllProductReviewsByLanguage(SiteContext.Current.WorkingLanguage.LanguageId);

				if (productReviews != null && productReviews.Count() > 0)
				{
					var productReviewIDs = productReviews.Select(e => e.ProductID).ToList();

					if (_ProductService == null)
					{
						_ProductService = new ProductService();
					}

					var data = (from p in _ProductService.GetAllProductsByLanguage(SiteContext.Current.WorkingLanguage.LanguageId)
								where productReviewIDs.Contains(p.ProductID)
								select new
								{
									ProductName = p.Name,
									ProductID = p.ProductID
								});

					GridProducts.DataSource = data;
					GridProducts.DataBind();
				}
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (SiteContext.Current == null)
			{
				Server.Transfer("~Administration/AdminLogin.aspx");
			}
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			//if(!IsPostBack)
			//{
			BindProducts();
			//}
		}

		protected void GridProductReviews_RowDeleting(object sender, GridViewDeleteEventArgs e)
		{
			var grid = sender as GridView;
			var hfProductReviewID = grid.Rows[e.RowIndex].FindControl("hfProductReviewID") as HiddenField;

			int theProductReviewID = int.Parse(hfProductReviewID.Value);

			if (_ProductReviewService == null)
			{
				_ProductReviewService = new Service.ProductReviewService();
			}

			if (_ProductReviewService.DeleteProductReview(theProductReviewID) > 0)
			{
				BindProducts();
			}
		}

		protected void GridProductReviews_RowUpdating(object sender, GridViewUpdateEventArgs e)
		{
			if (SiteContext.Current.WorkingLanguage != null)
			{
				var gridProductReview = sender as GridView;
				var hfProductReviewID = gridProductReview.Rows[e.RowIndex].FindControl("hfProductReviewID") as HiddenField;
				int theProductReviewID = int.Parse(hfProductReviewID.Value);

				if (_ProductReviewService == null)
				{
					_ProductReviewService = new Service.ProductReviewService();
				}

				var theCurrentProductReview = _ProductReviewService.GetAllProductReviewsByLanguage(SiteContext.Current.WorkingLanguage.LanguageId).Where(exp => exp.ProductReviewID == theProductReviewID).FirstOrDefault();

				var TextReply = gridProductReview.Rows[e.RowIndex].FindControl("TextReply") as TextBox;

				if (TextReply.Text.Trim().Length > 0)
				{
					theCurrentProductReview.ReplyText = TextReply.Text.Trim();
					theCurrentProductReview.RepliedOn = DateTime.Now;

					if (_ProductReviewService.UpdateProductReview(theCurrentProductReview) > 0)
					{
						BindProducts();
					}
				}
			}
		}

		protected void GridProductReview_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.DataItem != null)
			{
				var LinkButtonDelete = e.Row.FindControl("LinkButtonDelete") as LinkButton;
				var LiteralUserName = e.Row.FindControl("LiteralUserName") as Literal;
				var ButtonSubmitReply = e.Row.FindControl("ButtonSubmitReply") as Button;
				var UserID = e.Row.FindControl("UserID") as HiddenField;

				var dateItem = e.Row.DataItem as Data.Entity.ProductReview;

				var productreviewreplyinput = e.Row.FindControl("productreviewreplyinput") as System.Web.UI.HtmlControls.HtmlControl;
				var productreviewreply = e.Row.FindControl("productreviewreply") as System.Web.UI.HtmlControls.HtmlControl;

				if (string.IsNullOrEmpty(dateItem.ReplyText))
				{
					productreviewreplyinput.Visible = true;
					productreviewreply.Visible = false;
					ButtonSubmitReply.Text = GetLocalResourceString("Admin.ProductReview.ReplyButtonText");
				}
				else
				{
					productreviewreplyinput.Visible = false;
					productreviewreply.Visible = true;
				}

				var theUserID = new Guid(UserID.Value);

				LinkButtonDelete.Text = GetLocalResourceString("Admin.DeleteButtonText");

				var user = System.Web.Security.Membership.GetUser(theUserID);
				if (user != null)
				{
					LiteralUserName.Text = user.UserName;
				}
				else
				{
					var dataItem = e.Row.DataItem as Data.Entity.ProductReview;
					LiteralUserName.Text = dateItem.IPAddress;
				}
			}
		}

		protected void GridProducts_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.Header)
			{
				e.Row.Cells[0].Text = GetLocalResourceString("Admin.ProductList.NameHeader");
				e.Row.Cells[1].Text = GetLocalResourceString("Admin.ProductReview.ProductReviewHeader");
			}

			if (e.Row.DataItem != null)
			{
				var productID = (int)GridProducts.DataKeys[e.Row.RowIndex].Value;

				if (_ProductReviewService == null)
				{
					_ProductReviewService = new Service.ProductReviewService();
				}

				var productReviewData = _ProductReviewService.GetProductReviewsByProduct(productID);

				var gridProductReview = e.Row.FindControl("GridProductReviews") as GridView;
				gridProductReview.DataSource = productReviewData;
				gridProductReview.DataBind();

				var HyperLinkToProductDetail = e.Row.FindControl("HyperLinkToProductDetail") as HyperLink;

				HyperLinkToProductDetail.NavigateUrl = "~/ProductDetail.aspx?productid=" + productID.ToString();
			}
		}
	}
}